package practice.tmp;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class Solution113 {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> tmp = new LinkedList<>();

    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        if (root == null) return res;
        tmp.add(root.val);
        back(root, targetSum - root.val);
        return res;
    }

    public void back(TreeNode root, int targetSum) {
        if (root.left == null && root.right == null && targetSum == 0) {
            res.add(new ArrayList<>(tmp));
            return;
        }
        if (root.left != null) {
            tmp.add(root.left.val);
            back(root.left, targetSum - root.left.val);
            tmp.remove(tmp.size() - 1);
        }
        if (root.right != null) {
            tmp.add(root.right.val);
            back(root.right, targetSum - root.right.val);
            tmp.remove(tmp.size() - 1);
        }
    }
}
